#include "stdio.h"

class _generic_field_file_header {
 public:
  char file_id[60];
  char program_version[60];
  char creation_date[60];
  char endianess[4];
  int  ndim;
  int  box_size[10];
  long bytes_per_site;
  long sites;
  _generic_field_file_header() {
    strcpy(file_id, "File Type: MDP FIELD\n");
  };
};

int main(int argc, char **argv) {

  if(argc==1) {
    printf("Program to check the content of an MDP type file\n");
    printf("\nUsage: inspect [filename]\n\n");
    exit(0);
  };
  
  FILE *fp=fopen(argv[1], "r");
  
  // BEGIN setting up the file header
  int sites=1;
  _generic_field_file_header myheader;
  fread(&myheader, sizeof(char), 
	sizeof(_generic_field_file_header)/sizeof(char), fp);

  printf("\nFile header: %s\n", myheader.file_id);
  printf("File created by program: %s\n", myheader.program_version);
  printf("on Date:                 %s\n", myheader.creation_date);
  printf("Lattice n. of dimension: %i ( ", myheader.ndim);
  for(int j=0; j<myheader.ndim; j++) {
    sites*=myheader.box_size[j];
    printf("%i ", myheader.box_size[j]);
  };
  printf(")\n");
  printf("Bytes per site:          %i\n", myheader.bytes_per_site);
  printf("Number of sites:         %i\n", myheader.sites);
  if(sites!=myheader.sites) 
    printf("[This is not a regular lattice]\n");

  if(myheader.endianess[0]==0x12) 
    printf("[This file is in little endian format]\n");
  if(myheader.endianess[0]==0x43) 
    printf("[This file is in big endian format]\n");

  switch(myheader.bytes_per_site) {
  case 288:
    printf("[It could be a gauge configuration in single precision]\n");
    break;
  case 576:
    printf("[It could be a gauge configuration in double precision]\n");
    break;
  case 96:
    printf("[It could be a spinor configuration in single precision]\n");
    break;
  case 192:
    printf("[It could be a spinor configuration in double precision]\n");
    break;
  case 1152:
    printf("[It could be a propagator in single precision]\n");
    break;
  case 2304:
    printf("[It could be a propagator in double precision]\n");
    break;
  default:
    printf("[This is unknown stuff]\n");
    break;
  };
  fseek(fp,myheader.sites*myheader.bytes_per_site, SEEK_CUR);
  char alpha;
  do {
    fread(&alpha, 1, 1, fp);
    printf("%c",alpha);
  } while(!feof(fp));
  printf("\nEnd report!\n");
  fclose(fp);

  // END setting up header 
};

